package com.lry.web.fiction.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.lry.web.fiction.entity.Fiction;
import com.lry.web.fiction.entity.FictionUpdate;
import com.lry.web.fiction.mapper.FictionUpdateMapper;
import com.lry.web.fiction.service.FictionUpdateService;
import com.lry.web.fiction.entity.Fiction;
import com.lry.web.fiction.entity.FictionUpdate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FictionUpdateServiceImpl implements FictionUpdateService {
    @Autowired
    FictionUpdateMapper fictionUpdateMapper;


    public int setFictionUpdate(FictionUpdate fictionUpdate) {
        return fictionUpdateMapper.insert(fictionUpdate);
    }


    public List<FictionUpdate> getFictionUpdates() {
        return fictionUpdateMapper.selectList(null);
    }


    public int setFictionUpdateUp(FictionUpdate fictionUpdate) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("fiction_id",fictionUpdate.getFictionId());
        return fictionUpdateMapper.update(fictionUpdate,wrapper);
    }


    public int deleteByFictionId(String fictionId) {
        List<String> list=  Arrays.asList(fictionId.split(","));
        int i =0;
        for(String id:list){
            i = fictionUpdateMapper.deleteByFictionId(id);
        }
        return i;
    }

    @Override
    public int deleteByFId(String fId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("fiction_id",fId);
        return fictionUpdateMapper.delete(wrapper);
    }

    public int deleteByFVId(String fVId) {
        return fictionUpdateMapper.deleteByFVId(fVId);
    }

    @Override
    public Map<String, FictionUpdate> selectByFiction(List<Fiction> fictions) {
        Map<String, FictionUpdate> map = new HashMap<>();
        for(Fiction v: fictions){
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("fiction_id",v.getFictionId());
            map.put(v.getFictionId(),fictionUpdateMapper.selectOne(wrapper));
        }
        return map;

    }

    @Override
    public int updateDeleted(String fictionId) {
        return fictionUpdateMapper.updateDeleted(fictionId);
    }

}
